package cn.jhc.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.jhc.bean.ShopBean;
import cn.jhc.bean.UserBean;
import cn.jhc.dao.ShopDAO;
@WebServlet("/deletelistgoods.do")
public class DeleteListgoodsServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
	HttpSession session=req.getSession();
	req.setCharacterEncoding("UTF-8");
	List<ShopBean> listgoods = new ArrayList<ShopBean>();
	UserBean users = (UserBean) session.getAttribute("user");
	String id =req.getParameter("id");
	int Id = Integer.parseInt(id);
	String date=req.getParameter("date");
	String time=req.getParameter("time");
	double price=Double.parseDouble(req.getParameter("price"));
	int amount=Integer.parseInt(req.getParameter("amount"));
	String datetime=date+" "+time;
	ShopDAO dao=null;
	String user=users.getDisplayname();
	if (null != users) {
		dao=new ShopDAO();
		
		try{
		if(dao.Checklistgoods(user,datetime).size()>1)
		dao.deletelistgoods(Id, datetime,price,amount);
		else
			dao.deletelist(user,datetime);
		listgoods=dao.Checklistgoods(user,datetime);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}if(listgoods.size()==0)
	{
		RequestDispatcher dispatcher = req.getRequestDispatcher("/checklist.do");
		dispatcher.forward(req, resp);
	}else{
	req.setAttribute("listgoods", listgoods);
	RequestDispatcher dispatcher = req.getRequestDispatcher("/checklistgoods.jsp");
	dispatcher.forward(req, resp);}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
	doPost(req, resp);
}
}